package com.shengqi.xinnongren.work.ui.goods.presenter;

import com.google.gson.Gson;
import com.shengqi.xinnongren.common.base.BasePresenter;
import com.shengqi.xinnongren.common.config.network.Constants;
import com.shengqi.xinnongren.common.config.network.RetrofitHelper;
import com.shengqi.xinnongren.common.config.user.UserManager;
import com.shengqi.xinnongren.common.md5.SafeUtils;
import com.shengqi.xinnongren.common.utils.LogUtil;
import com.shengqi.xinnongren.work.ui.goods.model.ConfirmOrderBean;
import com.shengqi.xinnongren.work.ui.goods.view.ConfirmOrderView;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by LuoYaWei on 2018/7/31.
 */
public class ConfirmOrderPresenter extends BasePresenter<ConfirmOrderView> {

    /**
     * @param type 1 取货  2送货
     * @param list map key 规格id 规格数量
     */
    public void getOrderData(int type, List<Map<String, Object>> list) {
        getView().showLoadingView();
        Map<String, Object> map = new HashMap<>();
        map.put("type", type + "");
        map.put("skuIdList", new Gson().toJson(list));
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().getOrderData(requestBody), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {
                String data = SafeUtils.decrypt(respond.data);
                LogUtil.e("确认订单数据: " + data);
                ConfirmOrderBean bean = new Gson().fromJson(data, ConfirmOrderBean.class);
                getView().showOrderData(bean);
            } else {
                getView().onRequestFailed(respond.message);
            }
        });
    }

    public void createOrder(int type, String addressID, String timeID, String shoppingBagID, String couponID, String integral, String shoppingCarID, String remark, String totalMoney, String finalMoney) {

        Map<String, Object> map = new HashMap<>();
        map.put("type", type + "");
        map.put("addressId", addressID);
        map.put("storeId", UserManager.getInstance().getStoreId());
        map.put("storeTimeId", timeID);
        map.put("bagId", shoppingBagID);
        map.put("couponId", couponID);
        map.put("integral", integral);
        map.put("shoppingCarId", shoppingCarID);
        map.put("des", remark);
        map.put("paymentAmount", totalMoney);
        map.put("orderAmount", finalMoney);

        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().createOrder(requestBody), respond -> {
            if (respond.result == 1) {
                getView().showCreateOrderRes(respond);
            } else {
                getView().onRequestFailed(respond.message);
            }
        });


    }

    public void calculateMoney(String bagID, String couponID, String integral, List<Map<String, Object>> skuList) {
        Map<String, Object> map = new HashMap<>();
        map.put("storeId", UserManager.getInstance().getStoreId());
        map.put("bagId", bagID);
        map.put("couponId", couponID);
        map.put("integral", integral);
        map.put("skuIdList", new Gson().toJson(skuList));
        JSONObject aesJsonData = Constants.getAESJsonData(map, UserManager.getInstance().getToken());
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), aesJsonData.toString());
        addTask(RetrofitHelper.getInstance().getService().calculateOrderMoney(requestBody), respond -> {
            getView().hideLoadingView();
            if (respond.result == 1) {
                String data = SafeUtils.decrypt(respond.data);
                LogUtil.e("算钱结果: " + data);
                getView().showCalculateRes(data);
            } else {
                getView().onRequestFailed(respond.message);
            }
        });
    }
}
